package com.group5.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.group5.dao.GroupAndCustomerMapper;
import com.group5.dao.PlatformMapper;
import com.group5.dao.UserssMapper;
import com.group5.pojo.*;
import com.group5.service.BuyerService;
import com.group5.service.IntegerToJsonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/buyer")
public class BuyerController {

//    @Autowired
    @Autowired
    GroupAndCustomerMapper groupAndCustomerMapper;
    @Autowired
    UserssMapper userssMapper;

    @Autowired
    PlatformMapper platformMapper;
    BuyerService buyerService=new BuyerService();

    /**
     * 根据前端传过来的uname在user表中查找对应的oid
     * 若buyer_oid（bid）和seller_oid（sid）不相等：1.若根据bid和sid查找为空，则新建申请（将bid，sid，status=3写入数据库），返回1
     *                                2.若根据bid，sid查找不为空，若status==2则更新status=3。返回2
     *                                3.其他不操作，返回3
     * 若 bid==sid，则返回0
     * @param username
     * @param oid
     * @return
     */
    @PutMapping("/apply")
    public int buyerApplication(@RequestParam String username,@RequestParam int oid){
        int bid = userssMapper.getBuyerOidByUsername(username);
        List<GroupAndCustomer> groupAndCustomers = groupAndCustomerMapper.selectByExample(buyerService.selectByBuyerAndSellerOid(bid,oid));
        if (bid!=oid) {
            if(groupAndCustomers.isEmpty()){
                GroupAndCustomer groupAndCustomer = new GroupAndCustomer();
                groupAndCustomer.setSeller_oid(oid);
                groupAndCustomer.setBuyer_oid(bid);
                groupAndCustomer.setBuyer_application_status(3);
                groupAndCustomerMapper.insert(groupAndCustomer);
                return 1;
            }
            else if(groupAndCustomers.get(0).getBuyer_application_status()==2)
            {
                groupAndCustomers.get(0).setBuyer_application_status(3);
                groupAndCustomerMapper.updateByPrimaryKeySelective(groupAndCustomers.get(0));
                return 2;
            }
            else
                return 3;
        }
        else {
            return 0;
        }
    }

    /**
     * //todo（此方法现在可能没用）
     * 根据前端传过来的uname在user表中查找对应的oid
     *若buyer_oid和seller_oid查找为空，返回0
     * 若buyer_oid和seller_oid查找为非空：
     * buyer_application_status为2，前端传过来的condition为保存，则返回1
     * buyer_application_status为2，前端传过来的condition为提交，则更新buyer_application_status=3，返回2
     *其他，返回3
     * @param uname
     * @param sid
     * @param condition
     * @return
     */
    //TODO：只能提供卖方的oid和买方的uname
    @GetMapping("/commit")
    public int buyerCommit(@RequestParam String uname,@RequestParam int sid,@RequestParam String condition){
        int bid = userssMapper.getBuyerOidByUsername(uname);
        List<GroupAndCustomer> groupAndCustomers = groupAndCustomerMapper.selectByExample(buyerService.selectByBuyerAndSellerOid(bid,sid));
        if(!groupAndCustomers.isEmpty()){
            if(groupAndCustomers.get(0).getBuyer_application_status()==2&&condition.equals("保存")){
                return 1;
            }
            else if(groupAndCustomers.get(0).getBuyer_application_status()==2&&condition.equals("提交")){
                groupAndCustomers.get(0).setBuyer_application_status(3);
                groupAndCustomerMapper.updateByPrimaryKeySelective(groupAndCustomers.get(0));
                return 2;
            }
            else
                return 3;
        }

        return 0;



    }
    /**
     * 买方查询未申请或加入的卖方，由买方管理员调用
     *
     * @param username 买方管理员的username，需要在数据库查到管理员userss，根据userss的oid查询该买方可以申请的卖方
     * @return 卖方的列表
     */
    @GetMapping("/platforms/notjoined")
    public List<Platform> NotJoinedPlatforms(@RequestParam String username){
        Userss userss=userssMapper.selectByExample(buyerService.selectUserByUsernameExample(username)).get(0);

        return platformMapper.selectNotJoinedPlatform(userss.getOid());
    }

    /**
     * 买方删除自己的用户，由买方管理员调用，删除自己的一个用户，不能是买方管理员
     * uid为要删除的用户uid
     * 删除管理员时，返回2，数据库什么都不做
     * 删除普通用户时，返回1，数据库将对应用户改为临时用户状态
     * @param uid
     * @return
     */
    @PutMapping("/users/delete")
    public int DeleteBuyerUser(@RequestParam int uid){
        Userss userss=userssMapper.selectByPrimaryKey(uid);
        if(userss.getUser_authority()==3)
            return 2;
        else{
            buyerService.initUser(userss);
            return userssMapper.updateByPrimaryKey(userss);
        }
    }

    @PostMapping("/plantest")
    public int test(@RequestBody List<PurchaseList> purchaseLists){
        System.out.println(111);
       for(PurchaseList purchaseList:purchaseLists){
           System.out.println(purchaseList);
       }
       return 1;
    }

}
